package com.wc.算法提高课.F第六章_基础算法.排序.超快速排序;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/2 13:11
 * @description https://www.acwing.com/problem/content/109/
 */
public class Main {
    /**
     * 思路：
     * 每次交换至多改变一个逆序对的数量
     * 那么最少的操作次数也就是逆序对的数量
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 500010;
    static int[] tr = new int[N];
    static int[] a = new int[N], c = new int[N];
    static int n, len = 0;

    public static void main(String[] args) {
        while ((n = sc.nextInt()) != 0) {
            for (int i = 1; i <= n; i++) a[i] = c[i] = sc.nextInt();
            Arrays.sort(c, 1, n + 1);
            Arrays.fill(tr, 1, n + 1, 0);
            int j = 1;
            for (int i = 2; i <= n; i++) {
                if (c[i] != c[j]) c[++j] = c[i];
            }
            len = j;
            long res = 0;
            for (int i = n; i >= 1; i--) {
                int num = get(a[i]);
                add(num, 1);
                res += query(num - 1);
            }
            out.println(res);
        }
        out.flush();
    }

    static int get(int x) {
        int l = 1, r = len;
        while (l < r) {
            int mid = l + r + 1>> 1;
            if (c[mid] <= x) l = mid;
            else r = mid - 1;
        }
        return l;
    }

    static void add(int x, int c) {
        for (int i = x; i <= n; i += lowbit(i)) {
            tr[i] += c;
        }
    }


    static long query(int x){
        long res = 0;
        for(int i = x; i > 0; i -= lowbit(i)){
            res += tr[i];
        }
        return res;
    }

    static int lowbit(int x) {
        return x & -x;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
